
package games;

import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.Array;
import java.util.ArrayList;

import javax.swing.*;

/**
 * @author SamilCan
 */
public class GameFrame extends JFrame {
    public GameFrame(Game g) {
        game = g;

        initComponents();
        log = new StringBuilder("");
    }

    private void startActionPerformed(ActionEvent e) {
        // TODO add your code here
        Player p1 = new Player(game.gameBoard.board.elementAtDL(0),playerin1.getText());
        game.players.insertAtBeg(p1);
        Player p2 = new Player(game.gameBoard.board.elementAtDL(0),playerin2.getText());
        game.players.insertAtBeg(p2);
        Player p3 = new Player(game.gameBoard.board.elementAtDL(0),playerin3.getText());
        game.players.insertAtBeg(p3);
        Player p4 = new Player(game.gameBoard.board.elementAtDL(0),playerin4.getText());
        game.players.insertAtBeg(p4);
        //game.randomizeUserOrder();

        addLog("Let's play..");
        cur_player = game.players.elementAtDL(0);

        game.gameStart();
    }

    public void addLog(String s){
        log.append(System.getProperty("line.separator"));
        log.append(s);
        
        history.setText(log.toString());
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - SamilC an
        addPlayer = new JPanel();
        playerin1 = new JTextField();
        playerin2 = new JTextField();
        playerin3 = new JTextField();
        playerin4 = new JTextField();
        startgame = new JButton();
        background = new JLabel();
        history = new JLabel();

        //======== this ========
        Container contentPane = getContentPane();

        //======== addPlayer ========
        {

            // JFormDesigner evaluation mark
            addPlayer.setBorder(new javax.swing.border.CompoundBorder(
                new javax.swing.border.TitledBorder(new javax.swing.border.EmptyBorder(0, 0, 0, 0),
                    "JFormDesigner Evaluation", javax.swing.border.TitledBorder.CENTER,
                    javax.swing.border.TitledBorder.BOTTOM, new java.awt.Font("Dialog", java.awt.Font.BOLD, 12),
                    java.awt.Color.red), addPlayer.getBorder())); addPlayer.addPropertyChangeListener(new java.beans.PropertyChangeListener(){public void propertyChange(java.beans.PropertyChangeEvent e){if("border".equals(e.getPropertyName()))throw new RuntimeException();}});


            //---- startgame ----
            startgame.setText("start");
            startgame.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    startActionPerformed(e);
                }
            });

            GroupLayout addPlayerLayout = new GroupLayout(addPlayer);
            addPlayer.setLayout(addPlayerLayout);
            addPlayerLayout.setHorizontalGroup(
                addPlayerLayout.createParallelGroup()
                    .addGroup(addPlayerLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(addPlayerLayout.createParallelGroup()
                            .addGroup(addPlayerLayout.createSequentialGroup()
                                .addGroup(addPlayerLayout.createParallelGroup()
                                    .addComponent(playerin1)
                                    .addGroup(addPlayerLayout.createSequentialGroup()
                                        .addComponent(playerin2, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
                                        .addGap(0, 0, Short.MAX_VALUE)))
                                .addContainerGap())
                            .addGroup(GroupLayout.Alignment.TRAILING, addPlayerLayout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addGroup(addPlayerLayout.createParallelGroup()
                                    .addGroup(GroupLayout.Alignment.TRAILING, addPlayerLayout.createSequentialGroup()
                                        .addComponent(startgame)
                                        .addGap(23, 23, 23))
                                    .addGroup(GroupLayout.Alignment.TRAILING, addPlayerLayout.createSequentialGroup()
                                        .addComponent(playerin3, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
                                        .addContainerGap())))))
                    .addGroup(GroupLayout.Alignment.TRAILING, addPlayerLayout.createSequentialGroup()
                        .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(playerin4, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
            );
            addPlayerLayout.setVerticalGroup(
                addPlayerLayout.createParallelGroup()
                    .addGroup(addPlayerLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(playerin1, GroupLayout.PREFERRED_SIZE, 18, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(playerin2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(playerin3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(playerin4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addGap(12, 12, 12)
                        .addComponent(startgame)
                        .addContainerGap(15, Short.MAX_VALUE))
            );
        }
        /*location[19][0]=775;
        location[19][1]=790;*/
        //---- gameboard ----
        
        background.setLayout(null);
        
        int[][] location =new int[50][2];
        
        location[19][0]=775;
        location[19][1]=790;
        
        location[1][0]=180;
        location[1][1]=120;
        
        location[2][0]=250;
        location[2][1]=120;
        
        location[3][0]=320;
        location[3][1]=120;
        
        location[4][0]=447;
        location[4][1]=120;
        
        location[5][0]=510;
        location[5][1]=120;
        
        location[7][0]=600;
        location[7][1]=90;
        
        location[6][0]=645;
        location[6][1]=120;
        
        location[9][0]=790;
        location[9][1]=140;
        
        location[8][0]=740;
        location[8][1]=200;
        
        location[10][0]=740;
        location[10][1]=260;
        
        location[12][0]=740;
        location[12][1]=325;
        
        location[11][0]=740;
        location[11][1]=395;
        
        location[15][0]=740;
        location[15][1]=565;
        
        location[13][0]=740;
        location[13][1]=600;
        
        location[14][0]=740;
        location[14][1]=740;
        
        location[17][0]=735;
        location[17][1]=705;
        
        location[0][0]=110;
        location[0][1]=120;
        
        location[16][0]=650;
        location[16][1]=830;
        
        location[21][0]=585;
        location[21][1]=810;
        
        location[18][0]=515;
        location[18][1]=830;
        
        location[23][0]=465;
        location[23][1]=825;
        
        location[20][0]=300;
        location[20][1]=830;
        
        location[26][0]=245;
        location[26][1]=800;
        
        location[22][0]=170;
        location[22][1]=830;
        
        location[24][0]=115;
        location[24][1]=830;
        
        location[25][0]=5;
        location[25][1]=750;
        
        location[30][0]=5;
        location[30][1]=700;
        
        location[28][0]=5;
        location[28][1]=630;
        
        location[29][0]=5;
        location[29][1]=560;
        
        location[31][0]=5;
        location[31][1]=400;
        
        location[35][0]=5;
        location[35][1]=350;
        
        location[32][0]=5;
        location[32][1]=290;
        
        location[33][0]=5;
        location[33][1]=200;
        
        location[39][0]=5;
        location[39][1]=190;
        
        
        
        DLNode<Square> temp = game.gameBoard.board.elementAtDL(0);
        
        Insets insets=background.getInsets();
        
        
        //CircularDoublyLinkedList< JButton> butns = new CircularDoublyLinkedList< JButton>();
        for (int i =0; i< game.gameBoard.board.getSize();i++){
            
            temp.getData().setVisible(true);
            temp.getData().setText(temp.getData().name);
            
            Dimension size=temp.getData().getPreferredSize();
            temp.getData().setBounds(25+insets.left, 5+insets.top, size.width, size.height);
            temp.getData().setLocation(location[i][0], location[i][1]);
            temp.getData().setLayout(new FlowLayout());
            background.add(temp.getData());
            temp = temp.getNextNode();
            
            //System.out.println(c.gridx);
            
            //temp=gameBoard.board.elementAtDL(i);
            
        }
        
        //temp.getData().setLocation(50, 50);
        

        //background.add(new JButton("button"));


        //---- background ----
        background.setIcon(new ImageIcon("img/board.png"));
        background.setFocusable(false);
        

        //---- history ----
        history.setText("history");
        history.setVerticalAlignment(SwingConstants.TOP);

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(addPlayer, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(background)
                    .addGap(18, 18, 18)
                    .addComponent(history, GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)
                    .addContainerGap())
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(contentPaneLayout.createParallelGroup()
                            .addComponent(addPlayer, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addComponent(history, GroupLayout.PREFERRED_SIZE, 813, GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(30, Short.MAX_VALUE))
                .addGroup(contentPaneLayout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(background, GroupLayout.DEFAULT_SIZE, 822, Short.MAX_VALUE)
                        .addContainerGap())
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    DLNode<Player> cur_player;

    //History ---
    private StringBuilder log;

    //GameBoard --- Monopoly
    Game game;

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - SamilCan
    private JPanel addPlayer;
    private JTextField playerin1;
    private JTextField playerin2;
    private JTextField playerin3;
    private JTextField playerin4;
    private JButton startgame;
    private JLabel background;
    private JLabel history;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
